package com.su27.web;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DocServlet extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		InputStream is = req.getInputStream();
		byte[] buff = new byte[1024 * 10];
		is.read(buff);
		String reqStr = new String(buff);
		System.out.println("reqStr="+reqStr);
//		reqStr =  "{\"method\":\"login\",\"username\":\"zhangsan\",\"password\":\"01D7F40760960E7BD9443513F22AB9AF\",\"imsi\":\"abcddd\"}";
		
		DocReqBean reqBean = JSonUtil.parseJsonStr2Bean(reqStr);
		
		DocService service = new DocService();
		
		DocRespBean respBean = null;
		if ("login".equals(reqBean.getMethod())) {
			respBean = service.login(reqBean);
		} else if ("getFileList".equals(reqBean.getMethod())) {
			respBean = service.getFileList(reqBean);
		} else if ("getFile".equals(reqBean.getMethod())) {
			respBean = service.getFile(reqBean);
		} else if ("log".equals(reqBean.getMethod())) {
			respBean = service.log(reqBean);
		}
		String result = "{\"result\":false,\"info\":\"service fail\"}";
		if (respBean != null) {
			result = JSonUtil.parseBean2JsonStr(respBean);
//			result = URLEncoder.encode(result, "utf-8");
		}
		System.out.println("result = "+result);
		resp.setCharacterEncoding("utf-8");
		PrintWriter pw = resp.getWriter();
		pw.write(result);
		pw.flush();
		pw.close();
		
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req,resp);
	}
	
	

}
